<template>
  <div class="class-page">
    <el-card>
    <div class="header-title">班级管理</div>
    <div class="class-header">
      <el-select v-model="searchGrade" placeholder="年级" class="search-select">
        <el-option v-for="item in gradeOptions" :key="item" :label="item" :value="item" />
      </el-select>
      <el-select v-model="searchClass" placeholder="班级" class="search-select">
        <el-option v-for="item in classOptions" :key="item" :label="item" :value="item" />
      </el-select>
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button @click="handleReset">重置</el-button>
      <el-button type="primary" class="add-btn" @click="goAdd">+ 新增班级</el-button>
    </div>
    <el-alert :title="`查询结果：共计 ${total} 条数据`" type="info" show-icon class="result-alert" />
    <el-table :data="tableData" border stripe class="class-table">
      <el-table-column prop="grade" label="年级" width="120" />
      <el-table-column prop="className" label="班级名称" width="180" />
      <el-table-column prop="studentCount" label="班级人数" width="120" />
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-link type="primary" @click="goEdit(scope.row)">编辑</el-link>
          <el-link type="danger" @click="handleDelete(scope.row)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        v-model:current-page="current"
        v-model:page-size="size"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getClassList, deleteClass, type ClassData } from '@/api/class';

const router = useRouter();
const gradeOptions = ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级'];
const classOptions = ['(1)班', '(2)班', '(3)班', '(4)班', '(5)班'];
const searchGrade = ref('');
const searchClass = ref('');
const tableData = ref<ClassData[]>([]);
const total = ref(0);
const current = ref(1);
const size = ref(10);

const queryParams = reactive({
  grade: '',
  className: '',
  current: 1,
  size: 10
})

const fetchData = async () => {
  try {
    const res = await getClassList(queryParams)
    tableData.value = res.data.records
    total.value = res.data.total
  } catch (error) {
    console.error('获取班级列表失败:', error)
  }
}

const handleSearch = () => {
  queryParams.grade = searchGrade.value
  queryParams.className = searchClass.value
  queryParams.current = 1
  fetchData()
}

const handleReset = () => {
  searchGrade.value = ''
  searchClass.value = ''
  queryParams.grade = ''
  queryParams.className = ''
  queryParams.current = 1
  fetchData()
}

const handleSizeChange = (val: number) => {
  queryParams.size = val
  fetchData()
}

const handleCurrentChange = (val: number) => {
  queryParams.current = val
  fetchData()
}

function goAdd() {
  router.push('/main/class/add');
}

function goEdit(row: any) {
  router.push(`/main/class/edit/${row.id}`);
}

function handleDelete(row: any) {
  ElMessageBox.confirm('确认删除该班级信息吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteClass(row.id).then(() => {
      ElMessage.success('删除成功');
      fetchData();
    });
  });
}

onMounted(fetchData);
</script>

<style scoped>
.header-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 16px;
}
.class-page {
  padding: 24px;
  background: #f7f8fa;
  min-height: 100vh;
}
.class-header {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
}
.search-select {
  width: 160px;
  margin-right: 8px;
}
.add-btn {
  margin-left: auto;
}
.result-alert {
  margin-bottom: 16px;
}
.class-table {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
}
.el-link + .el-link {
  margin-left: 8px;
}
.pagination {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
}
</style>
